package com.jiniutech.common.interceptors;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@Slf4j
public class LogHandlerInterceptor implements HandlerInterceptor {
    private ThreadLocal<Long> time = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        time.set(System.currentTimeMillis());
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        log.info("path:" + request.getServletPath() + ",params:" + request.getParameterMap().toString() +
                "time:[" + (System.currentTimeMillis() - time.get()) + "ms],返回值:" + response.toString());
    }
}
